package com.hotel.service.user.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotel.dao.user.UserMapper;
import com.hotel.entity.Function;
import com.hotel.entity.Role;
import com.hotel.entity.User;
import com.hotel.entity.UserBusiness;
import com.hotel.service.function.FunctionService;
import com.hotel.service.role.RoleService;
import com.hotel.service.user.UserService;
import com.hotel.service.userbusiness.UserBusinessService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Resource
    private UserMapper userMapper;


    @Resource
    private RoleService roleService;

    @Resource
    private UserBusinessService userBusinessService;

    @Resource
    private FunctionService functionService;

    @Override
    public List<Function> getUserBtnStrById(String id) {
        String[] split = new String[0];
        //根据用户名查询出他的ID,在根据ID查询出关联的角色ID
        LambdaQueryWrapper<UserBusiness> lqwUserBusiness = new LambdaQueryWrapper();
        lqwUserBusiness.eq(UserBusiness::getKeyId,id);
        UserBusiness userBusiness = userBusinessService.getOne(lqwUserBusiness);
        //先根据type的key_id为这个userId.toString()拿到具体的角色集
        List<UserBusiness> userBusinessValue = userBusinessService.getBasicData(userBusiness.getKeyId(), "UserRole");
        //查询出相对应的权限ID（父）分离开来
        if (userBusinessValue != null && userBusinessValue.size() > 0) {
            String roleValue = userBusinessValue.get(0).getValue();
            if (roleValue != null) {
                roleValue = roleValue.replace(",", ""); //角色id-单个
                List<UserBusiness> roleFunctionsList = userBusinessService.getBasicData(roleValue, "RoleFunctions");//将该角色下的权限进行赋值
                if (roleFunctionsList != null && roleFunctionsList.size() > 0) {
                    String btnStr = roleFunctionsList.get(0).getValue();
                    if (btnStr != null) {
                        split = btnStr.split(",");
                    }
                }
            }
        }
        List<Function> functionList = functionService.getAllFunction();//获取所有功能模块
        List<Function> resultFunctionList = new ArrayList<>();
        List<Function> childrenList = null;  //子菜单
        Map<Long, Function> functionMap = new HashMap<>();
        for (Function function : functionList) {
            childrenList = functionService.findMenusByNumber(function.getNumber());   //所有父组件的子组件
            function.setFunctionList(childrenList);
            functionMap.put(function.getId(), function);
        }
        for (int i = 0; i < split.length; i++) {
            Long funId = Long.valueOf(split[i]);
            resultFunctionList.add(functionMap.get(funId));
        }
        return resultFunctionList;
    }
}




